package test;

import java.util.LinkedList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class 生产者消费者模型 {

    public static void main(String[] args) {
        Stock2 stock = new Stock2();
        Thread th1 = new Thread(() -> {
            int count = 0;
            while (true) {
                System.out.println("生产者生产了一个商品" + count++);
                // stack.push();这个方法很特殊，入股stack满了，就会阻塞住，直到有空闲为止
                stock.push("生产商品!" + count++);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread th2 = new Thread(() -> {
            while (true) {
                // stack.take();这个方法很特殊，stack为空，就会阻塞住，直到有元素为止
                String take = stock.take();
                System.out.println("消费者消费了一个商品：" + take);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        th1.start();
        th2.start();
    }
}

/**
 * 方法一：
 * 共享资源,synchronized,wait,notify,notifyAll
 */
class Stock {

    private LinkedList<String> productList = new LinkedList<>();

    private final int Max = 10;

    public synchronized void push(String goodName) {
        while (Max == productList.size()) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        productList.push(goodName);
        this.notifyAll();
    }

    public synchronized String take() {
        while (productList.isEmpty()) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String poll = productList.poll();
        this.notifyAll();
        return poll;
    }
}

/**
 * 方法二：
 * 共享资源,lock,condition->await,signal,signalAll
 */
class Stock2 {

    private LinkedList<String> productList = new LinkedList<>();

    private ReentrantLock lock = new ReentrantLock();
    private Condition full = lock.newCondition();
    private Condition empty = lock.newCondition();

    private final int Max = 10;

    public void push(String goodName) {
        try {
            lock.lock();
            while (productList.size() == Max) {
                System.out.println("生产商品111");
                full.await();
            }
            System.out.println("生产商品");
            productList.push(goodName);
            empty.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    public String take() {
        try {
            lock.lock();
            while (productList.isEmpty()) {
                empty.await();
            }
            String poll = productList.poll();
            full.signalAll();
            return poll;
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return null;
    }
}